/*
 * Copyright 2013 Sigurd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.client.controller.downloadtable;

import de.digiway.rapidbreeze.client.infrastructure.BusEvents;
import de.digiway.rapidbreeze.client.infrastructure.I18n;
import de.digiway.rapidbreeze.client.model.download.DownloadModel;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TableView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.stage.WindowEvent;
import org.rribbit.RRB;
import org.rribbit.RequestResponseBus;

/**
 *
 * @author Sigurd
 */
public class DownloadTableContextMenu extends ContextMenu {

    private ResourceBundle rb = I18n.getInstance().getResourceBundle();
    private TableView<DownloadModel> table;
    private MenuItem startItem;
    private MenuItem pauseItem;
    private MenuItem unpauseItem;
    private MenuItem deleteItem;
    private MenuItem deleteAllItem;
    private MenuItem removeFinishedItem;
    private MenuItemActionHandler actionHandler;

    DownloadTableContextMenu(TableView<DownloadModel> downloadTable) {
        super();
        this.table = downloadTable;

        this.actionHandler = new MenuItemActionHandler();

        setPrefWidth(150);
        setOnShowing(new MenuShowingHandler());

        startItem = new MenuItem(rb.getString("DownloadGridContext.Start"));
        pauseItem = new MenuItem(rb.getString("DownloadGridContext.Pause"));
        unpauseItem = new MenuItem(rb.getString("DownloadGridContext.Unpause"));
        deleteItem = new MenuItem(rb.getString("DownloadGridContext.Delete"));
        deleteAllItem = new MenuItem(rb.getString("DownloadGridContext.DeleteAll"));
        removeFinishedItem = new MenuItem(rb.getString("DownloadGridContext.RemoveFinished"));

        startItem.setAccelerator(new KeyCodeCombination(KeyCode.S));
        pauseItem.setAccelerator(new KeyCodeCombination(KeyCode.P));
        deleteItem.setAccelerator(new KeyCodeCombination(KeyCode.DELETE));

        startItem.setOnAction(actionHandler);
        pauseItem.setOnAction(actionHandler);
        unpauseItem.setOnAction(actionHandler);
        deleteItem.setOnAction(actionHandler);
        deleteAllItem.setOnAction(actionHandler);
        removeFinishedItem.setOnAction(actionHandler);

        getItems().add(startItem);
        getItems().add(pauseItem);
        getItems().add(unpauseItem);
        getItems().add(new SeparatorMenuItem());
        getItems().add(deleteItem);
        getItems().add(deleteAllItem);
        getItems().add(new SeparatorMenuItem());
        getItems().add(removeFinishedItem);
    }

    private class MenuItemActionHandler implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent t) {
            Object source = t.getSource();
            RequestResponseBus rrb = RRB.get();
            ObservableList<DownloadModel> selectedItems = table.getSelectionModel().getSelectedItems();

            if (source.equals(startItem)) {
                rrb.send(BusEvents.MENU_ITEM_START_EVENT, selectedItems);
            } else if (source.equals(pauseItem)) {
                rrb.send(BusEvents.MENU_ITEM_PAUSE_EVENT, selectedItems);
            } else if (source.equals(unpauseItem)) {
                rrb.send(BusEvents.MENU_ITEM_UNPAUSE_EVENT, selectedItems);
            } else if (source.equals(deleteItem)) {
                rrb.send(BusEvents.MENU_ITEM_DELETE_EVENT, selectedItems);
            } else if (source.equals(deleteAllItem)) {
                rrb.send(BusEvents.MENU_ITEM_DELETE_ALL_EVENT);
            } else if (source.equals(removeFinishedItem)) {
                rrb.send(BusEvents.CLEAR_FINISHED_DOWNLOADS);
            }
        }
    }

    private class MenuShowingHandler implements EventHandler<WindowEvent> {

        @Override
        public void handle(WindowEvent t) {
            ObservableList<DownloadModel> downloads = table.getSelectionModel().getSelectedItems();
            boolean showStart = false;
            boolean showPause = false;
            boolean showUnpause = false;
            boolean showRemoveFinished = false;
            for (DownloadModel download : downloads) {
                if (!download.isRunning() && !download.isFinished()) {
                    showStart = true;
                }
                if (download.isRunning()) {
                    showPause = true;
                }
                if (download.isPause()) {
                    showUnpause = true;
                }
                if (download.isFinished()) {
                    showRemoveFinished = true;
                }
            }

            startItem.setDisable(!showStart);
            pauseItem.setDisable(!showPause);
            unpauseItem.setDisable(!showUnpause);
            removeFinishedItem.setDisable(!showRemoveFinished);
        }
    }
}
